//
//  NewworkViewController.m
//  jiaxingOASystem
//
//  Created by michael on 16/6/12.
//  Copyright © 2016年 czd. All rights reserved.
//
//#import "OASelectDepartmentController.h"
//#import "NotonewsViewController.h"
#import "NewworkViewController.h"
//#import "OADetailCell.h"
#import "selectview.h"
#import "MHDatePicker.h"
#import "GDataDefines.h"
//#import "createDocmodel.h"
#import "OADepartmentModel.h"
#import "OADetailDispatchModel.h"
#import "huiyiViewController.h"


@interface huiyiViewController ()
@property(nonatomic,strong)OADetailDispatchModel * detailPatchModel ;
@property(nonatomic,strong)NSMutableArray * attachmentArray; //
@property(nonatomic,strong)NSMutableArray * departments;//发送
@property(nonatomic,strong)NSMutableArray * department;//保存

@property(nonatomic,copy)NSString * infoString; //服务器返回字段 根据该字段跳转不同的界面
@property(nonatomic,copy)NSString * infoString0;
@property(nonatomic,strong)NSMutableArray * subItems;  //选中 需要提交得数据
@property(nonatomic,strong)OADetailDispatchModel * detailModel ;

@property (strong, nonatomic)selectview * selectview;
@property (strong, nonatomic)UIButton * button1;
@property (strong, nonatomic)UIButton * button2;
@property (strong, nonatomic)UIButton * databutton;
@property (strong, nonatomic)UIButton * daybutton;
//@property (strong, nonatomic)  UITextField *TextField1;
@property (strong, nonatomic)  UITextView *nameField;
@property (strong, nonatomic)  UITextView *workplaceField;
@property (strong, nonatomic)  UITextView *dianhuaField;
@property (strong, nonatomic)  UITextView *mingcengField;
@property (strong, nonatomic)  UITextView *lingdaoField;
@property (strong, nonatomic)  UITextView *didianField;
@property (strong, nonatomic)  UITextView *jingfeiField;






@property (strong, nonatomic)  UITextView *personField;

@property (strong, nonatomic)  UITextView *dayField;

@property (strong, nonatomic)  UITextView *workField;
@property (strong, nonatomic)  UITextView *huodongcontextField;
@property (strong, nonatomic)  UITextView *TextField1;
@property (strong, nonatomic)  UITextView *guimoField;
@property(nonatomic,copy)NSString * body;
@property (strong, nonatomic) MHDatePicker *selectDatePicker;

@property(nonatomic,copy)NSString * typestring;

@property(nonatomic,copy)NSString * shifoustring;
@property(nonatomic,copy)NSString * timebeforestring;
@property(nonatomic,copy)NSString * timeafterstring;
@property(nonatomic,copy)NSString * timestring;//时间

@property(nonatomic,copy)NSString * datastring;//日期


@property(nonatomic,copy)NSString * zhiwustring;//职务

//办公室
@property (nonatomic,copy)NSString *namestring;
@property (nonatomic,copy)NSString *bumenstring;
@property (nonatomic,copy)NSString *dianhuatring;
@property (nonatomic,copy)NSString *mingcengtring;
@property (nonatomic,copy)NSString *lingdaotring;
@property (nonatomic,copy)NSString *huodongtring;
@property (nonatomic,copy)NSString *didiantring;
@property (nonatomic,copy)NSString *jingfeitring;


@property (nonatomic,copy)NSString *shiyoustring;
@property (nonatomic,copy)NSString *personstring;
@property (nonatomic,copy)NSString *daystring;

// 按钮
@property (nonatomic,copy)NSString *anniu;

@end

@implementation huiyiViewController
-(NSMutableArray*)attachmentArray{
    if (_attachmentArray ==nil) {
        _attachmentArray = [NSMutableArray array];
    }
    return _attachmentArray;
}
-(NSMutableArray*)subItems{
    if (_subItems ==nil) {
        _subItems = [NSMutableArray array];
    }
    return _subItems;

}

- (void)viewDidLoad {
    [super viewDidLoad];
     NSDate *currentDate = [NSDate date];//获取当前时间，日期
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-dd "];
_datastring = [dateFormatter stringFromDate:currentDate];


    [self _initView];
    [self requestdata];
    self.view.backgroundColor = [UIColor whiteColor];
    //点击手势
 UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(keybordDown)];
    [self.view addGestureRecognizer:tap];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(btnClick:) name:@"2" object:nil];
    
    // Do any additional setup after loading the view.
}

- (void)btnClick:(NSNotification *)noti{

NSString *str = noti.userInfo[@"text"];
_anniu = str;
}


- (void)keybordDown {

    
    [self.view endEditing:YES];
        }
- (void)requestdata{
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray * array = @[@{@"userId":userId}];
    [HttpTools postWithURL:KexchdataURL arrays:array method:@"createDoc" success:^(id JSON) {
    NSLog(@"=====%@",JSON);
   
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        self.detailModel = [self getDetailModel:JSON];
        
//        _namestring = _detailModel.name;
//        _bumenstring = _detailModel.workname;
//        _personstring = _detailModel.person;
//        _daystring = _detailModel.data;
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    }];


}
//获取解析得到的数据。
-( OADetailDispatchModel *)getDetailModel:(NSString*)dataString;
{
    NSData * data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
    OADetailDispatchModel * detailModel = [[OADetailDispatchModel alloc]init];
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData: data options:0 error:nil];
    //获取根节点（Users）
    GDataXMLElement *rootElement = [doc rootElement];
    //获取根节点下的节点（User）
    NSArray *rootData= [rootElement elementsForName:@"数据"];
    for (GDataXMLElement *subData in rootData) {
        NSArray * tempArray = [subData elementsForName:@"数据要素"];
        for (GDataXMLElement *data in tempArray) {
            GDataXMLElement *documentElement= [[data elementsForName:@"GwOrigUnit"] objectAtIndex:0];
//            detailModel.name = [documentElement stringValue];
//            
//            
//            
//           GDataXMLElement *filestep= [[data elementsForName:@"GwSubject"] objectAtIndex:0];
//            detailModel.workname = [filestep stringValue];
//             GDataXMLElement *filestep1= [[data elementsForName:@"GwFileClass"] objectAtIndex:0];
//            detailModel.person = [filestep1 stringValue];
//             GDataXMLElement *filestep2= [[data elementsForName:@"GwRegister"] objectAtIndex:0];
//            detailModel.data = [filestep2 stringValue];
            
            GDataXMLElement *fileId= [[data elementsForName:@"唯一标识"] objectAtIndex:0];
            detailModel.fileId = [fileId stringValue];
            
        }
    }
    
    
    
    return detailModel;
    
}

- (void)_initView{
_tableView  = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, KScreenWidth, KScreenHeight-47-64) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;

UIButton *savebutton = [UIButton buttonWithType:UIButtonTypeCustom];
[savebutton setTitle:@"保存" forState:UIControlStateNormal];
[savebutton setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
savebutton.frame = CGRectMake(0, _tableView.bottom, .5*KScreenWidth, 47);

UIButton *cilcuationbutton = [UIButton buttonWithType:UIButtonTypeCustom];
[cilcuationbutton setTitle:@"发送" forState:UIControlStateNormal];
cilcuationbutton.frame = CGRectMake(savebutton.right, _tableView.bottom, .5*KScreenWidth, 47);
[cilcuationbutton setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
savebutton.backgroundColor = [UIColor whiteColor];
cilcuationbutton.backgroundColor = [UIColor whiteColor
];
[savebutton addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside];
[cilcuationbutton addTarget:self action:@selector(cilcuationAction) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:savebutton];
[self.view addSubview:cilcuationbutton];
 
[self.view addSubview:_tableView];

}
- (void)saveAction{
 [self keybordDown];
    
    if ([self.workplaceField.text length] == 0) {
        [self.view makeToast:@"对不起，申请部门为空"];
        return;
    }else if ([self.nameField.text length] == 0) {
        [self.view makeToast:@"对不起，联系人为空"];
        return;
    }else if ([self.dianhuaField.text length] == 0) {
        [self.view makeToast:@"对不起，联系电话为空"];
        return;
    }else if ([_mingcengField.text length] == 0) {
        [self.view makeToast:@"对不起，活动名称为空"];
        return;
    }else if ([_lingdaoField.text length] == 0) {
        [self.view makeToast:@"对不起，拟邀请省领导为空"];
        return;
    }else if ([_huodongcontextField.text length] == 0) {
        [self.view makeToast:@"对不起，活动内容(议程)为空"];
        return;
    }else if ([_guimoField.text length] == 0) {
        [self.view makeToast:@"对不起，活动规模(人数)为空"];
        return;
    }else if ([_didianField.text length] == 0) {
        [self.view makeToast:@"对不起，活动地点为空"];
        return;
    }
//    else if ([_jingfeiField.text length] == 0) {
//        [self.view makeToast:@"对不起，活动经费(元为空"];
//        return;
//    }



   
else{




_timestring = [NSString stringWithFormat:@"%@到%@",_timebeforestring,_timeafterstring];
_timestring = [_timestring stringByReplacingOccurrencesOfString:@"年" withString:@"-"];
_timestring = [_timestring stringByReplacingOccurrencesOfString:@"月" withString:@"-"];
_timestring = [_timestring stringByReplacingOccurrencesOfString:@"日" withString:@""];



NSString *json =[NSString stringWithFormat: @"{'department':'%@','linkman':'%@', 'phone':'%@','activeName':'%@','personName':'%@','activeDate':'%@','activePersonNum':'%@','activeAddress':'%@','activeMoney':'%@'}",_workplaceField.text,_nameField.text,_dianhuaField.text, _mingcengField.text,_lingdaoField.text,_datastring,_guimoField.text,_didianField.text,_jingfeiField.text];
 NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];





    NSArray *array =@[@{@"sName":userId},@{@"json":json}];
    NSLog(@"1");


        [HttpTools postWithURL:Kexchcreate arrays:array method:@"createConference"  success:^(id JSON) {
//    [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
//     NSData * data = [JSON dataUsingEncoding:NSUTF8StringEncoding];
//     GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData: data options:0 error:nil];
//    GDataXMLElement *rootElement = [doc rootElement];
//      
//          NSArray *infoData= [rootElement elementsForName:@"info"];
//    for (int i = 0;i<[infoData count];i++)
//    {
//        GDataXMLElement *infoElement = [infoData objectAtIndex:i];
//        self.infoString0 = [infoElement stringValue];
//    }
_infoString0 = [NSString stringWithFormat:@"%@",JSON];
    if (self.infoString0) { //成功发送，提交下一办理人
        
        [self.view.window makeToast:@"保存成功"];
              _type = @"ok";

    }
          
    else{
    
    [self.view.window makeToast:@"保存失败"];
    }
   } failure:^(NSError *error) {
        [self.view.window makeToast:@"保存失败"];
    }];
}
}
- (void)cilcuationAction{
 [self keybordDown];
    if([_type isEqualToString:@"ok"]){
  self.body = [NSString stringWithFormat:@"&lt;?xml version=\'1.0\' encoding=\'utf-8\'?&gt;\n"
                     "&lt;交换 xmlns=\'exch.jhsx.gov.cn/data\' xmlns:xsi=\'http://www.w3.org/2001/XMLSchema-instance\'&gt;\n"
                     "&lt;数据&gt;\n"
                     "&lt;数据元素&gt;\n"
                     "&lt;/数据元素&gt;\n"
                     "&lt;数据实体&gt;\n"
                     "&lt;附件&gt;\n"
                     "&lt;/附件&gt;\n"
                     "&lt;/数据实体&gt;\n"
                     "&lt;/数据&gt;\n"
                     "&lt;/交换&gt;"];
        NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
           
     NSArray * array = @[@{@"unid":self.infoString0},@{@"userId":userId},@{@"famxml":self.body}];

    
    

 [HttpTools postWithURL:KexchdataURL arrays:array method:KSetSubmitWorkMethod success:^(id JSON) {
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        self.departments = [self dataWith:JSON];
        
        if ([self.infoString isEqualToString:@"11"]) { //成功发送，提交下一办理人
            
            [UIAlertView showWithTitle:@"提示" message:@"发送成功" cancelButtonTitle:@"确定" otherButtonTitles:nil tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshOADetailData" object:nil];
                [self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];
            }];
            
        }else if ([self.infoString isEqualToString:@"12"]) { //返回环节信息
//            OASelectDepartmentController * selectVC = [[OASelectDepartmentController alloc]init];
//            selectVC.subItems = self.subItems;
//            selectVC.body = self.body;
//                        selectVC.fileId = _infoString0;
//
//            selectVC.detailPatchModel = self.detailPatchModel;
//            selectVC.attachmentArray = self.attachmentArray;
//            selectVC.departments = self.departments;
//            [self.navigationController pushViewController:selectVC animated:YES];
 FSSelectDepartmentController * selectVC = [[FSSelectDepartmentController alloc]init];
            selectVC.subItems = self.subItems;
            selectVC.body = self.body;
        selectVC.fileId = _infoString0;

//            selectVC.detailPatchModel = self.detailPatchModel;
            selectVC.attachmentArray = self.attachmentArray;
            
//存储cell显示的内容
            selectVC.departments = self.departments;
            
            [self.navigationController pushViewController:selectVC animated:YES];
        }else if ([self.infoString isEqualToString:@"13"]) {//返回人员列表
            //            OASelectPersonController * selectPersonVC = [[OASelectPersonController alloc]init];
            //            selectPersonVC.personArray = self.personArray;
            //            selectPersonVC.body = self.body;
            //            selectPersonVC.setpString = self.setpString;
            //            if (self.detailModel ) {
            //                selectPersonVC.fileId = self.detailModel.fileId;
            //            }else if (self.detailPatchModel){
            //                selectPersonVC.fileId = self.detailPatchModel.fileId;
            //            }
            //
            //            [self.navigationController pushViewController:selectPersonVC animated:YES];
            
            
            
        }else if ([self.infoString isEqualToString:@"15"]){ //返回树
            //            OASelectPersonSecondController * selectPersonVC = [[OASelectPersonSecondController alloc]init];
            //            selectPersonVC.personArray = self.personArray;
            //            selectPersonVC.body = self.body;
            //            selectPersonVC.setpString = self.setpString;
            //            if (self.detailModel ) {
            //                selectPersonVC.fileId = self.detailModel.fileId;
            //            }else if (self.detailPatchModel){
            //                selectPersonVC.fileId = self.detailPatchModel.fileId;
            //            }
            //            selectPersonVC.groupArray = self.groupArray;
            //            selectPersonVC.rowArray = self.rowArray;
            //            [self.navigationController pushViewController:selectPersonVC animated:YES];
           
    
//    [self.view makeToast:@"归档成功"];
//    [self.navigationController popViewControllerAnimated:YES];
//     ComingworkongController *comvc =(ComingworkongController *) [self.storyboard instantiateViewControllerWithIdentifier:@"comvc"];
//    [self.navigationController pushViewController:comvc animated:YES];
    }
            
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
    }];


}
else{

        [self.view.window makeToast:@"请选择保存"];


}

}
#pragma mark 数据解析

-(NSMutableArray*)dataWith:(NSString*)dataString
{
    NSData * data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableArray *arrData = [[NSMutableArray alloc]init];
    NSMutableArray * subData = [NSMutableArray array];
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData: data options:0 error:nil];
    GDataXMLElement *rootElement = [doc rootElement];
    NSArray *users = [rootElement children];
    for (int i = 0;i<[users count];i++) {
        GDataXMLElement *ele = [users objectAtIndex:i];
        if ([[ele name] isEqualToString:@"环节"]){
            OADepartmentModel * model = [[OADepartmentModel alloc]init];
            model.department = [[ele attributeForName:@"流向"] stringValue];
            for (int j=0; j<[ele childCount]; j++) {
                GDataXMLElement  *subItem = [ele childAtIndex:j];
                [subData addObject:subItem.stringValue];
            }
            [arrData addObject:model];
        }
    }
    self.subItems = subData; //选中 需要提交得数据
    NSArray *infoData= [rootElement elementsForName:@"info"];
    for (int i = 0;i<[infoData count];i++)
    {
        GDataXMLElement *infoElement = [infoData objectAtIndex:i];
        self.infoString = [infoElement stringValue];
    }
    
    return arrData;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


   if (indexPath.row==0) {

  static NSString *cellid0 = @"cell0";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellid0];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid0];
           UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, cell.contentView.height)];

  label.text = @"申请部门 :";
    
    label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
      _workplaceField = [[UITextView alloc]init];
      _workplaceField.frame = CGRectMake(label.right, label.top+10, KScreenWidth-120, cell.height-10);
      UIColor *myColor = [UIColor colorWithWhite:0.5 alpha:0.2];
      _workplaceField.backgroundColor = myColor;
 [_workplaceField setFont:[UIFont systemFontOfSize:13]];
 _workplaceField.textAlignment = NSTextAlignmentLeft;
      _workplaceField.delegate = self;
       [cell.contentView addSubview:_workplaceField];
       }
               _workplaceField.text = _bumenstring;

             return cell;

}
else if (indexPath.row==1) {
 static NSString *cellid1 = @"cell1";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellid1];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid1];
          UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, cell.contentView.height)];
        
      label.text = @"联系人 :";
    
    label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
      _nameField = [[UITextView alloc]init];
      _nameField.frame = CGRectMake(label.right, label.top+10, KScreenWidth-120, cell.height-10);
      UIColor *myColor = [UIColor colorWithWhite:0.5 alpha:0.2];
      _nameField.backgroundColor = myColor;
 [_nameField setFont:[UIFont systemFontOfSize:13]];
 _nameField.textAlignment = NSTextAlignmentLeft;
      _nameField.delegate = self;
       [cell.contentView addSubview:_nameField];
       }
               _nameField.text = _namestring;

             return cell;
}
else if (indexPath.row==2) {
static NSString *cellid2 = @"cell2";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellid2];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid2];
      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, 30)];
       label.text = @"联系电话 :";
    
    label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
      _dianhuaField = [[UITextView alloc]init];
      _dianhuaField.frame = CGRectMake(label.right, label.top+10, KScreenWidth-120, cell.height-10);
      UIColor *myColor = [UIColor colorWithWhite:0.5 alpha:0.2];
      _dianhuaField.backgroundColor = myColor;
 [_dianhuaField setFont:[UIFont systemFontOfSize:13]];
 _dianhuaField.textAlignment = NSTextAlignmentLeft;
      _dianhuaField.delegate = self;
       [cell.contentView addSubview:_dianhuaField];
       }
               _dianhuaField.text = _dianhuatring;

             return cell;
}

else if (indexPath.row==3) {
    static NSString *cellid3 = @"cell3";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellid3];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid3];
      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, cell.contentView.height)];
    label.text = @"活动名称 :";
    
    label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
      _mingcengField = [[UITextView alloc]init];
      _mingcengField.frame = CGRectMake(label.right, label.top+10, KScreenWidth-120, cell.height-10);
      UIColor *myColor = [UIColor colorWithWhite:0.5 alpha:0.2];
      _mingcengField.backgroundColor = myColor;
 [_mingcengField setFont:[UIFont systemFontOfSize:13]];
 _mingcengField.textAlignment = NSTextAlignmentLeft;
      _mingcengField.delegate = self;
       [cell.contentView addSubview:_mingcengField];
       }
               _mingcengField.text = _mingcengtring;

             return cell;

}
else if (indexPath.row==4) {
 static NSString *cellid4 = @"cell4";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellid4];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid4];
         UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, cell.contentView.height)];
    label.text = @"拟邀请省领导 :";
    label.numberOfLines = 0;
    label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
      _lingdaoField = [[UITextView alloc]init];
      _lingdaoField.frame = CGRectMake(label.right, label.top+10, KScreenWidth-120, cell.height-10);
      UIColor *myColor = [UIColor colorWithWhite:0.5 alpha:0.2];
      _lingdaoField.backgroundColor = myColor;
 [_lingdaoField setFont:[UIFont systemFontOfSize:12]];
      _lingdaoField.delegate = self;
       [cell.contentView addSubview:_lingdaoField];
       }
             _lingdaoField.text = _lingdaotring;

             return cell;




}else if (indexPath.row==5) {
static NSString *cellid5 = @"cell5";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellid5];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid5];
              UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, cell.contentView.height)];

      label.text = @"活动内容(议程) :";
    label.numberOfLines = 0;
    label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
      _huodongcontextField = [[UITextView alloc]init];
      _huodongcontextField.frame = CGRectMake(label.right, label.top+10, KScreenWidth-120, cell.height-10);
      UIColor *myColor = [UIColor colorWithWhite:0.5 alpha:0.2];
      _huodongcontextField.backgroundColor = myColor;
 [_huodongcontextField setFont:[UIFont systemFontOfSize:12]];
      _huodongcontextField.delegate = self;
       [cell.contentView addSubview:_huodongcontextField];
       }
             _huodongcontextField.text = _huodongtring;

             return cell;
}else if (indexPath.row==6) {
static NSString *cellid6 = @"cell6";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellid6];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid6];
      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 80, cell.contentView.height)];
    label.text = @"活动时间 :";
    label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
 _databutton = [UIButton buttonWithType:UIButtonTypeCustom];
      _databutton.frame = CGRectMake(label.right, label.top, 120, label.height);
//    _button1.backgroundColor = [UIColor yellowColor];
    _databutton.titleLabel.font = [UIFont systemFontOfSize: 13.0];
    
                 [_databutton setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
           [_databutton addTarget:self action:@selector(dataaction) forControlEvents:UIControlEventTouchUpInside];

 
       [cell.contentView addSubview:_databutton];
       }
             [_databutton setTitle:_datastring forState:UIControlStateNormal];

             return cell;

}
else if (indexPath.row==7) {
static NSString *cellid7 = @"cell7";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellid7];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid7];
      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, cell.contentView.height)];
    label.text = @"活动规模(人数) :";
    _guimoField.keyboardType = UIKeyboardTypeDecimalPad;

        label.numberOfLines = 0;

    label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
      _guimoField = [[UITextView alloc]init];
      _guimoField.frame = CGRectMake(label.right, label.top+10, KScreenWidth-120, cell.height-10);
          _guimoField.keyboardType = UIKeyboardTypeDecimalPad;

      UIColor *myColor = [UIColor colorWithWhite:0.5 alpha:0.2];
      _guimoField.backgroundColor = myColor;
 [_guimoField setFont:[UIFont systemFontOfSize:12]];
      _guimoField.delegate = self;
       [cell.contentView addSubview:_guimoField];
       }
             return cell;
    
}
else if (indexPath.row==8){

static NSString *cellid8 = @"cell8";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellid8];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid8];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, cell.contentView.height)];
    label.text = @"活动地点 :";
    label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
    _didianField = [[UITextView alloc]init];
      _didianField.frame = CGRectMake(label.right, label.top+10, KScreenWidth-120, cell.height-10);
      UIColor *myColor = [UIColor colorWithWhite:0.5 alpha:0.2];
      _didianField.backgroundColor = myColor;
 [_didianField setFont:[UIFont systemFontOfSize:12]];
      _didianField.delegate = self;
       [cell.contentView addSubview:_didianField];
       }
       _didianField.text = _didiantring;
             return cell;
             }

else {

static NSString *cellid9 = @"cell9";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellid9];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid9];      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, cell.contentView.height)];
    label.text = @"活动经费(元) :";
    label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
    _jingfeiField = [[UITextView alloc]init];
      _jingfeiField.frame = CGRectMake(label.right, label.top+10, KScreenWidth-120, cell.height-10);
      _jingfeiField.keyboardType = UIKeyboardTypeDecimalPad;

      UIColor *myColor = [UIColor colorWithWhite:0.5 alpha:0.2];
      _jingfeiField.backgroundColor = myColor;
 [_jingfeiField setFont:[UIFont systemFontOfSize:12]];
      _jingfeiField.delegate = self;
       [cell.contentView addSubview:_jingfeiField];
       }
       _jingfeiField.text = _jingfeitring;
             return cell;

}

}
- (void)textViewDidChange:(UITextView *)textView
{
if(textView ==_nameField){
_namestring = _nameField.text;
}
if(textView ==_workplaceField){
_bumenstring = _workplaceField.text;
}

if(textView ==_workField){
_zhiwustring = _workField.text;
}

if(textView ==_huodongcontextField){
_shiyoustring = _huodongcontextField.text;
}
if(textView ==_personField){
_personstring = _personField.text;
}

if(textView ==_personField){
_daystring = _personField.text;
}

}
- (void)action1{
 _selectDatePicker = [[MHDatePicker alloc] init];
    _selectDatePicker.isBeforeTime = YES;
    _selectDatePicker.datePickerMode = UIDatePickerModeDate;
    
    __weak typeof(self) weakSelf = self;
    [_selectDatePicker didFinishSelectedDate:^(NSDate *selectedDate) {
//        NSString *string = [NSString stringWithFormat:@"%@",[NSDate dateWithTimeInterval:3600*8 sinceDate:selectedDate]];
//        weakSelf.myLabel2.text = string;
      NSString *string = [weakSelf dateStringWithDate:selectedDate DateFormat:@"yyyy年MM月dd日"];
      _timebeforestring = string;
      [weakSelf.button1 setTitle:string forState:UIControlStateNormal];
    }];

}
- (void)dataaction{
[self.view endEditing:YES];
 _selectDatePicker = [[MHDatePicker alloc] init];
    _selectDatePicker.isBeforeTime = YES;
    _selectDatePicker.datePickerMode = UIDatePickerModeDate;
    
    __weak typeof(self) weakSelf = self;
    [_selectDatePicker didFinishSelectedDate:^(NSDate *selectedDate) {
      NSString *string = [weakSelf dateStringWithDate:selectedDate DateFormat:@"yyyy年MM月dd日"];
      string = [string stringByReplacingOccurrencesOfString:@"年" withString:@"-"];
string = [string stringByReplacingOccurrencesOfString:@"月" withString:@"-"];
string = [string stringByReplacingOccurrencesOfString:@"日" withString:@""];
     _datastring = string;
      [weakSelf.databutton setTitle:string forState:UIControlStateNormal];
    }];
    

}

- (void)dataaction2{
 _selectDatePicker = [[MHDatePicker alloc] init];
    _selectDatePicker.isBeforeTime = YES;
    _selectDatePicker.datePickerMode = UIDatePickerModeDate;
    
    __weak typeof(self) weakSelf = self;
    [_selectDatePicker didFinishSelectedDate:^(NSDate *selectedDate) {
      NSString *string = [weakSelf dateStringWithDate:selectedDate DateFormat:@"yyyy年MM月dd日"];
      _datastring = string;
      [weakSelf.databutton setTitle:string forState:UIControlStateNormal];
    }];

}
- (void)action2{
_selectDatePicker = [[MHDatePicker alloc] init];
    _selectDatePicker.isBeforeTime = YES;
    _selectDatePicker.datePickerMode = UIDatePickerModeDate;
    
    __weak typeof(self) weakSelf = self;
    [_selectDatePicker didFinishSelectedDate:^(NSDate *selectedDate) {
//        NSString *string = [NSString stringWithFormat:@"%@",[NSDate dateWithTimeInterval:3600*8 sinceDate:selectedDate]];
//        weakSelf.myLabel2.text = string;
      NSString *string = [weakSelf dateStringWithDate:selectedDate DateFormat:@"yyyy年MM月dd日"];
        _timeafterstring = string;
      [weakSelf.button2 setTitle:[NSString stringWithFormat:@"到%@",string] forState:UIControlStateNormal];
    }];

}
- (NSString *)dateStringWithDate:(NSDate *)date DateFormat:(NSString *)dateFormat
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:dateFormat];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
    NSString *str = [dateFormatter stringFromDate:date];
    return str ? str : @"";
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{



//if (indexPath.row ==2) {
//    return 70 ;
//}else{
return 44;
//}
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
//if (textView == _guimoField||textView == _TextField1||textView == _dayField) {
//    [UIView animateWithDuration:0.2 animations:^{
//    self.view.transform = CGAffineTransformMakeTranslation(0, -100);
//    }];
//
//}
UIView *view = textView.superview;

while (![view isKindOfClass:[UITableViewCell class]]) {

view = [view superview];

}

UITableViewCell *cell = (UITableViewCell*)view;

CGRect rect = [cell convertRect:cell.frame toView:self.view];

if (rect.origin.y / 2 + rect.size.height>=KScreenHeight-216-46-64-47) {

self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 216, 0);

[self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];

}





  
    return YES;
}


- (void)textViewDidEndEditing:(UITextView *)textView{
//self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);

//if (textView == _guimoField||textView == _TextField1||textView == _dayField) {
        [UIView animateWithDuration:0.2 animations:^{
         self.view.transform = CGAffineTransformIdentity;
    }];

//}
    
   
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [_TextField1 resignFirstResponder];
    
    [_guimoField resignFirstResponder];
    
    [self.view endEditing:YES];
    return YES;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

 [UIView animateWithDuration:0.2 animations:^{
         self.view.transform = CGAffineTransformIdentity;
    }];
    return YES;
}


@end
